<?php

//靓号分类
namespace App\Http\Controllers\Admin\Mall;


use App\Enums\MallEnum;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;
use App\Response\Response;
use Illuminate\Http\Request;

class GoodCodeCateController extends BaseController
{
    /**
     * @api {Get} /mall/good-code-cate/list 靓号分类列表/下拉框
     *
     * @apiGroup 个性商城-靓号管理
     *
     * @apiParam {Int} cate_id1 靓号父分类id，0：不限制，其它根据接口
     * @apiParam {Int} cate_id2 靓号子分类id，0：不限制，其它根据接口
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": [
    {
    "id": 12, 父分类id
    "title": "S级号", 父分类名称
    "sub_cates": [ 子分类信息：
    {
    "id": 13, 子分类id
    "title": "S级" 子分类名称
    }
    ]
    },
    {
    "id": 15,
    "title": "叠字号",
    "sub_cates": []
    },
    {
    "id": 16,
    "title": "爱情号",
    "sub_cates": []
    }
    ]
    }
     */
    public function list(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'cate_id1' => 'integer|min:0',
                'cate_id2' => 'integer|min:0',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        $cateId1 = $request->cate_id1;
        $cateId2 = $request->cate_id2;

        $cateId1Where = $cateId2Where = [];
        if ($cateId1) {
            $cateId1Where = ['id' => $cateId1];
        }
        if ($cateId2) {
            $cateId2Where = ['id' => $cateId2];
        }
        $list = app('model_mall_categories')
            ->select('id', 'title')
            ->where($cateId1Where)
            ->where('parent_id', MallEnum::PRODUCT_CATE_ID_GOOD_CODE)
            ->get();
        foreach ($list as &$v) {
            $v->sub_cates = app('model_mall_categories')
                ->select('id', 'title')
                ->where($cateId2Where)
                ->where('parent_id', $v->id)
                ->get();
        }
        return Response::sendData($list);
    }

    /**
     * @api {Post} /mall/good-code-cate/add 新增靓号分类
     *
     * @apiGroup 个性商城-靓号管理
     *
     * @apiParam {Int} parent_cate_id 上级分类id，顶级分类传0，必须
     * @apiParam {String} good_code_name 靓号名称，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": []
    }
     */
    public function add(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'parent_cate_id' => 'required|integer',
                'good_code_name' => 'required|string|max:10',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        return app('repo_good_code')->addOrEditCate($request->all());
    }

    /**
     * @api {Post} /mall/good-code-cate/edit 编辑靓号分类
     *
     * @apiGroup 个性商城-靓号管理
     *
     * @apiParam {Int} cate_id 分类id，必须
     * @apiParam {Int} parent_cate_id 上级分类id，顶级分类传0，必须
     * @apiParam {String} good_code_name 靓号名称，必须
     *
     * @apiSuccessExample {json} Response-Example
     * {
    "code": 200,
    "data": []
    }
     */
    public function edit(Request $request)
    {
        //参数校验
        $v = app('validator')->make($request->all(),
            [
                'cate_id' => 'required|integer|min:1',
                'parent_cate_id' => 'required|integer',
                'good_code_name' => 'required|string|max:10',
            ]);
        if ($v->fails()) throw new BasicException(10001, $v->errors()->first());

        return app('repo_good_code')->addOrEditCate($request->all());
    }
}